package com.link.base.tencent.youtu.controller;

import com.link.core.cllog.LCLogger;
import com.link.base.tencent.youtu.service.YoutuService;
import com.link.core.basic.service.ServiceException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 优图人脸识别-内部
 *
 * @author ouyangzhenqiang
 * @date 2018/12/29
 */
@Controller
@RequestMapping("link/face")
public class YoutuController {
    @Resource
    public YoutuService youtuService;

    /**
     * 用户在人脸识别接待过程中，综合考虑系统匹配度&人眼识别后，选择提供的可选客户时，需要为选中的person进行人脸更新
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     */
    @RequestMapping(value = "/refreshPersonFace", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> refreshPersonFace(HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        LCLogger.info().withMessageKey("refreshPersonFace").withMessage("人脸识别接待 => 更新接待person人脸...").flush();

        try {
            String personId = request.getParameter("personId");
            if (StringUtils.isBlank(personId)) {
                throw new ServiceException("YOUTU-001");
            }
            String receptionId = request.getParameter("receptionId");
            if (StringUtils.isBlank(receptionId)) {
                throw new ServiceException("YOUTU-002");
            }
            youtuService.refreshPersonFace(personId,receptionId);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 用户在人脸识别接待过程中，综合考虑系统匹配度&人眼识别后，不选择提供的可选客户时，需要进行个体&人脸新建
     *
     * @author ouyangzhenqiang
     * @date 2018/12/29
     */
    @RequestMapping(value = "/newPersonFace", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> newPersonFace(HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        LCLogger.info().withMessageKey("newPersonFace").withMessage("人脸识别接待 => 新建接待person人脸...").flush();
        try {
            String receptionId = request.getParameter("receptionId");
            if (StringUtils.isBlank(receptionId)) {
                throw new ServiceException("YOUTU-002");
            }
            String personLnkId = youtuService.newPersonFace(receptionId);
            result.put("success", true);
            result.put("personLnkId", personLnkId);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

}
